package linuxbox.calendar.taglib;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.TimeZone;

import linuxbox.BoxStatic;
import linuxbox.util.StringUtil;
import linuxbox.calendar.logic.BoxCalendarEntry;
import linuxbox.calendar.logic.BoxCalendar;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class CalendarEntry
    extends TagSupport
{
  protected boolean showTime = true;
  protected boolean shortTime = false;
  protected boolean showDelete = true;
  protected TimeZone timeZone = null;
  protected int viewerID = -1;
  protected BoxCalendarEntry bean = null;

  public int doStartTag() throws JspException
  {
    try
    {
      JspWriter out = pageContext.getOut();

      BoxCalendar home = BoxCalendar.getInstance();
      if (viewerID == bean.getUserID()
          || (bean.getSharing() == home.BOXCALENDAR_PUBLIC))
      {
        out.print("<a href=\"");
        out.print(BoxStatic.servletDir);
        out.print("BoxCalendarAction?SubmitEdit=edit&id=");
        out.print(bean.getID());
        out.print("\">");
        if (showTime)
        {
          if (shortTime)
          {
            out.print(StringUtil.getDateTimeFormatString(timeZone, BoxStatic.SHORT_HOUR_FORMAT, bean.getFromTime()));
          }
          else
            out.print(home.getTimeRangeString(bean, timeZone));
          out.print("&nbsp;");
        }
        out.print(bean.getSubject());
        out.print("</a>");
        if (bean.getRepeat())
        {
          out.print("&nbsp;<IMG SRC=\"");
          out.print(BoxStatic.imageDir);
          out.print("cal_repeat.gif\" WIDTH=13 HEIGHT=12 BORDER=0 >");
        }
        if ( showDelete && (viewerID == bean.getUserID()))
        {
          out.print("<a href=\"");
          out.print(BoxStatic.servletDir);
          out.print("BoxCalendarAction?SubmitDelete=del&id=");
          out.print(bean.getID());
          out.print("\">");
          out.print("<img SRC=\"");
          out.print(BoxStatic.imageDir);
          out.print("cal_del.gif\" WIDTH=12 align=right HEIGHT=12 BORDER=0 Onclick='return confirmDel(\"");
          out.print(bean.getSubject());
          out.print(" ");
          out.print(home.getTimeRangeString(bean, timeZone));
          out.print("\")'  ALT=\"Delete\"></a>");
        }
      }
      else
      {
        if (bean.getSharing() == home.BOXCALENDAR_BUSY)
        {
          if (showTime)
          {
            if (shortTime)
            {
              out.print(StringUtil.getDateTimeFormatString(timeZone, BoxStatic.SHORT_HOUR_FORMAT, bean.getFromTime()));
            }
            else
              out.print(home.getTimeRangeString(bean, timeZone));
            out.print("&nbsp;");
          }
          out.print("Busy");
        }
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
      throw new JspException(e.getMessage());
    }

    return SKIP_BODY;
  }

  public void setshowtime(String aValue)
  {
    showTime = new Boolean(aValue).booleanValue();
  }

  public void setshorttime(String aValue)
  {
    shortTime = new Boolean(aValue).booleanValue();
  }

  public void settimezone(TimeZone tz)
  {
    timeZone = tz;
  }

  public void setshowdelete(String aValue)
  {
    showDelete = new Boolean(aValue).booleanValue();
  }

  public void setviewerid(int aValue)
  {
    viewerID = aValue;
  }

  public void setcalendarentry(BoxCalendarEntry bean0)
  {
    bean = bean0;
  }
}
